package com.lzw.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.anno.Log;
import com.lzw.pojo.DormitoryAdmin;
import com.lzw.service.admin.DormitoryAdminManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.List;

@RestController
@RequestMapping("/dormitoryAdminManagement")
@CrossOrigin(origins = "http://localhost:7070")
public class DormitoryAdminManagementController {

    @Autowired
    private DormitoryAdminManagementService dormitoryAdminManagementService;

    //全部查询
    @GetMapping("/listAll")
    public PageInfo<DormitoryAdmin> listAll(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize );
        List<DormitoryAdmin> list = dormitoryAdminManagementService.listAll();
        return new PageInfo<>(list);
    }

    // 根据条件查询
    @GetMapping("/list")
    public PageInfo<DormitoryAdmin> listDormitoryAdmins(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name, String gender, Integer dormitoryBuildingId) {
        PageHelper.startPage(pageNum, pageSize );
        List<DormitoryAdmin> list = dormitoryAdminManagementService.listDormitoryAdmins(pageNum,pageSize,name,gender,dormitoryBuildingId);
        return new PageInfo<>(list);
    }

    // 添加公寓管理员
    @Log
    @PostMapping("/add")
    public ResponseEntity<String> addDormitoryAdmin(@RequestBody DormitoryAdmin dormitoryAdmin) {
        try {
            dormitoryAdminManagementService.addDormitoryAdmin(dormitoryAdmin);
            return ResponseEntity.ok("添加成功");
        } catch (IllegalArgumentException e){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }

    // 删除公寓管理员
    @Log
    @DeleteMapping("/delete/{ids}")
    public String deleteDormitoryAdmin(@PathVariable List<Integer> ids) {
        dormitoryAdminManagementService.deleteDormitoryAdmin(ids);
        return "公寓管理员删除成功";
    }

    //添加公寓管理员
    @Log
    @PutMapping("/update")
    public ResponseEntity<String> updateDormitoryAdmin(@RequestBody DormitoryAdmin dormitoryAdmin) {
        try {
            dormitoryAdminManagementService.updateDormitoryAdmin(dormitoryAdmin);
            return ResponseEntity.ok("更新成功");
        } catch (IllegalArgumentException e){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }

    // 根据ID查询公寓管理员信息
    @GetMapping("/get/{id}")
    public DormitoryAdmin getDormitoryAdminById(@PathVariable("id") int id) {
        return dormitoryAdminManagementService.getDormitoryAdminById(id);
    }

}
